package com.xinqi.modules.sys.log.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.modules.sys.log.convert.OperatorLogConvert;
import com.xinqi.modules.sys.log.domain.OperatorLogEntity;
import com.xinqi.modules.sys.log.dto.req.OperatorLogQueryDTO;
import com.xinqi.modules.sys.log.dto.rsp.OperatorLogResultDTO;
import com.xinqi.modules.sys.log.manager.OperatorLogManager;
import com.xinqi.modules.sys.log.service.OperatorLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Map;

/**
 * 操作日志记录服务层实现
 *
 * @author: yyzhen
 * @date: 2023/08/23
 */
@Validated
@Service("operatorLogService")
@RequiredArgsConstructor
public class OperatorLogServiceImpl implements OperatorLogService {

    private final OperatorLogConvert operatorLogConvert;

    private final OperatorLogManager operatorLogManager;

    /**
     * 分页查询 操作日志记录数据
     */
    @Override
    @DS("slaver")
    public PageInfo<OperatorLogResultDTO> page(Param pageable, OperatorLogQueryDTO query) {
        //排序
        if (CollectionUtils.isEmpty(pageable.getSorts())) {
            pageable.setSorts(new ArrayList<>());
        }
        pageable.getSorts().add("ol.operator_time,DESC");
        return Pages.convert(operatorLogManager.findPage(Pages.page(pageable), query));
    }

    /**
     * 新增 操作日志记录数据
     */
    @Override
    public Long insert(Map<String, Object> map) {
        OperatorLogEntity entity = operatorLogConvert.create(map);
        entity.setOperatorTime(LocalDateTime.now());
        operatorLogManager.save(entity);
        return entity.getId();
    }

}
